// pages/skip/skip.js
//获取应用实例
const app = getApp();
const utils = require('../../utils/util.js');
const post = utils.post;
const jumpWeapp = utils.appid;
const sdk = require('../../utils/sdk.js')
const getCurrentPageUrlWithArgs = utils.getCurrentPageUrlWithArgs;

Page({
    data: {
        scene: '',
        appid: '',
        path: '',
        uid: '',
        weappid: '',
        picurl: '',
        desc: '',
        get_user_info: 0,
        play_time_start: 0,
        play_time_end: 0,
        play_time: 0,
        pls: '',
        flag: 'none',
        name: 'APP',
        jump_time_start: 0,
        jump_time_end: 0,
        jump_play_time: 0,

        weapp_play_time: 30, // 自己试玩时间设置
        weapp_jump_play_time: 20, // 分享玩时间设置
        btnbg: 'background-color: #1AAD19;color: #ffffff;',
        bor_r: ' border-top-left-radius: 20rpx;border-top-right-radius: 20rpx',
        ifQuestion: false,
        get_key: '',
        aid: '',
        question: '',
        question_id: '',
        yj_type: 1,
        answer: '请输入您的答案…',
        ifshz: true,
        download_apk: 0,
        ifsq: false,
        jumpWeapp: '',
        official: false,
        down_test: '',
        ts_test: '',
        fsw: false,
        issystem: true,
        isPreviewImage: false,
        service_Modal: false
    },
    onLoad: function(options) {
        let that = this;
        var reg = RegExp(/testweapp.whwangdoudou.cn/);
        if (reg.exec(utils.url)) {
            that.setData({
                official: true
            })
        };
        var ua = app.globalData.ua;
        if (/^ios/.test(ua)) {
            that.setData({
                issystem: false
            });
        } else if (/^android/.test(ua)) {
            that.setData({
                issystem: true
            });
        }
        if (!!options.jfplay) {
            that.setData({
                jfplay: options.jfplay,
            });
        }
        that.setData({
            jumpWeapp: jumpWeapp
        });
        app.getLbs();
        if (options.scene) {
            that.data.scene = options.scene;
            let _scene = decodeURIComponent(that.data.scene).split(':');
            that.data.uid = _scene[0];
            that.data.weappid = _scene[1];
            wx.setStorageSync('weapp_uid', that.data.uid);
            wx.removeStorageSync('pls_' + that.data.uid + '_' + that.data.weappid);
            if (_scene.length >= 3) {
                let _pls = _scene[2];
                if (_pls) {
                    that.data.pls = _pls;
                    wx.setStorageSync('pls_' + that.data.uid + '_' + that.data.weappid, that.data.pls);
                    that.setData({
                        flag: 'flex',
                        name: '公众号',
                        fsw: true
                    });
                }
            }
        } else {
            if (options.uid && options.weappid) {
                that.data.uid = options.uid;
                that.data.weappid = options.weappid;
                wx.setStorageSync('weapp_uid', that.data.uid);
            }
            wx.removeStorageSync('pls_' + that.data.uid + '_' + that.data.weappid);
            if (options.pls) {
                that.data.pls = options.pls;
                wx.setStorageSync('pls_' + that.data.uid + '_' + that.data.weappid, that.data.pls);
                that.setData({
                    flag: 'flex',
                    name: 'APP',
                    fsw: true
                });
                if (options.from && 'jlist' == options.from) {
                    that.setData({
                        flag: 'flex',
                        name: ''
                    });
                }
            }
        }
        that.fetchData();
    },
    upload() {
        let that = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
            success: res => {
                var tempFilesSize = res.tempFiles[0].size; //获取图片的大小，单位B
                let tempFilePaths = res.tempFilePaths;
                var pullImg = that.data.tempFilePaths
                if (tempFilesSize <= 2 * 1024 * 1024) {
                    // wx.showToast({
                    //     title: '正在上传...',
                    //     icon: 'loading',
                    //     mask: true,
                    //     duration: 1000
                    // });
                    // for (var i = 0; i < tempFilePaths.length; i++) {
                    //     that.ulFile(tempFilePaths[i])
                    //     pullImg.push(tempFilePaths[i])
                    // }
                    that.setData({
                        tempFilePaths: tempFilePaths[0],
                    });
                } else { //图片大于2M，弹出一个提示框
                    wx.showToast({
                        title: '上传图片不能大于2M!',
                        icon: 'none'
                    })
                }
            }
        })
    },
    upload_sc() {
        let that = this;
        var time = Date.now()
        time = parseInt(time / 1000)
        var w_uid = wx.getStorageSync("weapp_uid");
        let data = {
            type: 'upload',
            upload_time: time,
            m_uid: w_uid,
            aid: that.data.aid,
            weappid: that.data.weappid,
            step: that.data.step
        };
        wx.showLoading({
            title: "正在上传···",
            mask: true,
            success: (result) => {}
        });
        wx.uploadFile({
            url: utils.domain + '/review_image_upload.php',
            filePath: that.data.tempFilePaths,
            formData: data,
            name: 'file',
            success(res) {
                var aaa = JSON.parse(res.data)
                if (aaa.r == 0) {
                    wx.showModal({
                        title: '提示',
                        content: '提交成功!',
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '提交失败,请重新提交!',
                    })
                }
            },
            fail(res) {
                wx.showModal({
                    title: '提示',
                    content: '提交失败,请重新提交!',
                })
            },
            complete(res) {
                wx.hideLoading();
            }
        })
    },
    launchAppError: function(e) {
        wx.showToast({
            title: e.detail.errMsg,
            icon: 'none',
        });
    },

    bindsuccess(e) {
        let that = this;

        // if (that.data.pls) { // 自己试玩
        //     that.data.play_time_start = parseInt(new Date().getTime() / 1000);
        //     wx.setStorageSync('play_time_start_' + that.data.uid + '_' + that.data.weappid, that.data.play_time_start);

        // } else {
        //     that.data.jump_time_start = parseInt(new Date().getTime() / 1000);
        //     wx.setStorageSync('jump_time_start_' + that.data.uid + '_' + that.data.weappid, that.data.jump_time_start);
        // }
        that.handleLeave()
    },
    bindfail(e) {
        // wx.showToast({
        //     title: 'bindfail'
        // })
        // console.log('bindfail');
    },
    bindcomplete(e) {
        // wx.showToast({
        //     title: 'bindcomplete'
        // })
        // console.log('bindcomplete');
    },

    fetchData() {
        let that = this;
        let weapp_openid = wx.getStorageSync('weapp_openid');
        if (that.data.uid && that.data.weappid) {
            let uid = that.data.uid;
            let weappid = that.data.weappid;
            let p = {
                m: 'weapp_info',
                uid: uid,
                weappid: weappid
            };
            post(p).then(res => {
                // console.log(res);
                if (res.r == 0) {
                    that.setData({
                        page_ma: res.result.page_ma,
                        jump_weapp_appid: res.result.jump_weapp_appid,
                        appid: res.result.appid,
                        picurl: res.result.img_bg,
                        desc: res.result.desc,
                        get_user_info: res.result.get_user_info,
                        jump_weapp: res.result.jump_weapp,
                        zgbs: res.result.step
                    });
                    if (res.result.step > 1) {
                        that.setData({
                            allStep: res.result.weapp_steps
                        });
                    }
                    if (res.result.yj_type == 3) {
                        if (res.result.download_apk == 0) {
                            that.setData({
                                down_test: '点击"点击进入"启动小程序试玩小程序并找到问题的答案提交答案，领取奖励'
                            });
                        } else {
                            that.setData({
                                down_test: '点击"点击进入"完成相应任务并找到问题的答案提交答案，领取奖励'
                            });
                        }
                        //回答问题验证
                        that.setData({
                            yj_type: res.result.yj_type,
                            question: res.result.question,
                            question_id: res.result.question_id,
                        });
                        if (!!res.result.review_status) {
                            // res.result.review_status = 2
                            if (res.result.review_status == 0) {
                                let r_answer = res.result.answer
                                that.setData({
                                    ifQuestion: false,
                                    answer: r_answer
                                });
                                wx.showModal({
                                    content: '审核失败，请重新试玩！',
                                    showCancel: false,
                                    success(res) {}
                                });
                            } else if (res.result.review_status == 1) {
                                that.setData({
                                    yj_type: 1,
                                    bor_r: 'border-radius:20rpx;'
                                });
                                wx.showModal({
                                    content: '任务完成',
                                    showCancel: false,
                                    success(res) {}
                                });
                            } else if (res.result.review_status == 2) {
                                that.setData({
                                    ifshz: false,
                                    ifQuestion: true,
                                    answer: res.result.answer,
                                    btnbg: 'background-color:#fff;color:#1AAD19;border: 1px solid #1AAD19;'
                                });
                            }
                        }

                    } else if (res.result.yj_type == 2) {
                        //上传图片验证
                        if (res.result.download_apk == 0) {
                            that.setData({
                                down_test: '点击"点击进入"启动小程序试玩小程序并截图返回上传图片后，领取奖励',
                                ts_test: '请上传试玩小程序截图！'
                            });
                        } else {
                            that.setData({
                                down_test: '点击"点击进入"完成相应任务截图返回上传图片后，领取奖励',
                                ts_test: '完成相应任务截图上传！'
                            });

                        }
                        that.setData({
                            yj_type: res.result.yj_type
                        });
                        var weapp_steps = res.result.weapp_steps;
                        var ok = 0;
                        var fail = 0;
                        var shenhe = 0;
                        for (var i = 0; i < weapp_steps.length; i++) {
                            that.setData({
                                step: weapp_steps[i].step
                            });
                            if (weapp_steps[i].review_status) {
                                if (weapp_steps[i].review_status == 0) {
                                    that.setData({
                                        ifQuestion: true,
                                        desc: weapp_steps[i].desc,
                                        tempFilePaths: weapp_steps[i].image,
                                        review_status: weapp_steps[i].review_status,
                                    });
                                    if (weapp_steps[i].step == 1) {
                                        that.setData({
                                            ifstep: '0',
                                            ifQuestion: false,
                                        });
                                    }
                                    fail = weapp_steps[i].step
                                    break
                                } else if (weapp_steps[i].review_status == 1) {
                                    ok++
                                    that.setData({
                                        desc: weapp_steps[i].desc,

                                        ifQuestion: true,
                                        review_status: weapp_steps[i].review_status,
                                        ifstep: weapp_steps[i].step
                                    });
                                    if (weapp_steps[i].step == that.data.zgbs) {
                                        that.setData({
                                            ifQuestion: false,
                                        });
                                    }
                                    var aa
                                    if (res.result.step == 1) {
                                        aa = '任务完成!'
                                    } else {
                                        if (res.result.step == weapp_steps[i].step) {
                                            aa = '任务完成，请返回试玩其它任务！'
                                        } else {
                                            aa = '第' + weapp_steps[i].step + '步任务完成，请继续完成剩余步骤！'
                                        };
                                    }
                                } else if (weapp_steps[i].review_status == 2) {
                                    that.setData({
                                        desc: weapp_steps[i].desc,
                                        tempFilePaths: weapp_steps[i].image,
                                        ifshz: false,
                                        ifQuestion: true,
                                        btnbg: 'background-color:#fff;color:#1AAD19;border: 1px solid #1AAD19;',
                                        review_status: weapp_steps[i].review_status
                                    });
                                    if (weapp_steps[i].step == 1) {
                                        that.setData({
                                            ifstep: '0'
                                        });
                                    }
                                    shenhe = weapp_steps[i].step;
                                    break
                                }
                            } else {
                                if (weapp_steps[i].step == 1) {
                                    that.setData({
                                        ifstep: '0'
                                    });
                                }
                                that.setData({
                                    desc: weapp_steps[i].desc,
                                    tempFilePaths: '',
                                });
                                break;
                            }
                        };
                        if (fail > ok) {
                            if (res.result.step == 1) {
                                wx.showModal({
                                    content: '审核失败，请重新试玩！',
                                    showCancel: false,
                                    success(res) {}
                                });
                            } else {
                                wx.showModal({
                                    content: '第' + fail + '步审核失败，请重新试玩！',
                                    showCancel: false,
                                    success(res) {}
                                });
                            }
                        } else {
                            if (shenhe > ok) {
                                wx.showModal({
                                    content: '第' + shenhe + "步任务审核中…",
                                    showCancel: false,
                                    success(res) {}
                                });
                            } else {
                                if (ok > 0) {
                                    wx.showModal({
                                        content: aa,
                                        showCancel: false,
                                        success(res) {}
                                    });
                                }
                            }
                        }
                        // if (!!res.result.review_status) {
                        //     // res.result.review_status = 2
                        //     if (res.result.review_status == 0) {
                        //         that.setData({
                        //             ifQuestion: false,
                        //             tempFilePaths: res.result.image
                        //         });
                        //         wx.showModal({
                        //             content: '审核失败，请重新试玩！',
                        //             showCancel: false,
                        //             success(res) {}
                        //         });
                        //     } else if (res.result.review_status == 1) {
                        //         that.setData({
                        //             yj_type: 1,
                        //             bor_r: 'border-radius:20rpx;'
                        //         });
                        //         wx.showModal({
                        //             content: '任务完成',
                        //             showCancel: false,
                        //             success(res) {}
                        //         });
                        //     } else if (res.result.review_status == 2) {
                        //         that.setData({
                        //             tempFilePaths: res.result.image
                        //         });
                        //         that.setData({
                        //             ifshz: false,
                        //             ifQuestion: true,
                        //             btnbg: 'background-color:#fff;color:#1AAD19;border: 1px solid #1AAD19;'
                        //         });
                        //     }
                        // } else {
                        //     // that.setData({
                        //     //     // ifshz: true,
                        //     //     ifupload: true,
                        //     //     // aid: res.aid,
                        //     //     btnbg: 'background-color:#fff;color:#1AAD19;border: 1px solid #1AAD19;'
                        //     // });
                        // }

                    } else if (res.result.yj_type == 1) {
                        that.setData({
                            bor_r: 'border-radius:20rpx;'
                        });
                    }

                    that.data.weapp_play_time = parseInt(res.result.play_time);
                    that.data.weapp_jump_play_time = parseInt(res.result.jump_play_time);
                    if (res.result.download_apk == 1) {
                        if (!!weapp_openid) {
                            that.setData({
                                path: '/pages/webview/webview?path=' + res.result.page
                            })
                        }
                        that.setData({
                            download_apk: 1,
                            appid: res.result.appid,
                            picurl: res.result.img_bg,
                            get_user_info: res.result.get_user_info
                        });
                    } else if (res.result.download_apk == 2) {
                        that.setData({
                            download_apk: 2,
                        })
                    } else {
                        var _path = res.result.page;
                        if (_path.indexOf('?') != -1) { // 包含问号
                            _path += '&uid=' + that.data.uid + '&weappid=' + that.data.weappid + '&aid=' + res.aid;
                        } else { // 不包含问号
                            _path += '?uid=' + that.data.uid + '&weappid=' + that.data.weappid + '&aid=' + res.aid;
                        }
                        var pathT = '/pages/skip/skip?uid=' + that.data.uid + '&weappid=' + that.data.weappid + '&from=jlist'
                        let pls = wx.getStorageSync('pls_' + that.data.uid + '_' + that.data.weappid);
                        var pathT
                        if (pls) {
                            pathT = 'pages/jump/jump?pls=wx&uid=' + that.data.uid + '&weappid=' + that.data.weappid + '&from=xlist'
                        } else {
                            pathT = 'pages/jump/jump?uid=' + that.data.uid + '&weappid=' + that.data.weappid + '&from=xlist'
                        }

                        if (!!weapp_openid) {
                            that.setData({
                                appid: res.result.appid,
                                path: _path,
                            })
                        }
                        that.setData({
                            pathT: pathT,
                            picurl: res.result.img_bg,
                            get_user_info: res.result.get_user_info,
                        });
                    }
                }
            });
        }
    },
    handleGetUserInfo() {
        let that = this;
        // 2.获取用户信息
        wx.login({
            success(res) {
                var code = res.code;
                let weapp_openid = wx.getStorageSync('weapp_openid')
                wx.getUserInfo({
                    success(res2) {
                        var encryptedData = res2.encryptedData;
                        var iv = res2.iv;
                        var p = {
                            m: 'jump_get_userinfo',
                            code: code,
                            encryptedData: encryptedData,
                            iv: iv,
                            uid: that.data.uid,
                            weappid: that.data.weappid
                        };
                        // let pls = wx.getStorageSync('pls_' + that.data.uid + '_' + that.data.weappid);
                        // if (pls) {
                        //     p.type = 'play';
                        // } else {
                        //     p.type = 'jump_back';
                        // }
                        //if (0 == that.data.jfplay) {
                        post(p).then(res3 => {
                                // console.log(res3);
                                if (0 == res3.r) {
                                    var aid = res3.aid;
                                    // var _path = that.data.path;
                                    // if (_path.indexOf('?') != -1) { // 包含问号
                                    //     _path += '&aid=' + aid;
                                    // } else { // 不包含问号
                                    //     _path += '?aid=' + aid;
                                    // }
                                    // that.setData({
                                    //     path: _path,
                                    // });

                                    var userinfo = res3.userinfo;
                                    var weapp_openid = userinfo.openId;
                                    wx.setStorageSync('weapp_openid', weapp_openid); // openId
                                    wx.setStorageSync('_userinfo', res3.userinfo);
                                    wx.setStorageSync('device_info', wx.getSystemInfoSync());
                                    if (0 == that.data.download_apk) {
                                        if (!!that.data.page_ma) {
                                            that.ios_method()
                                        }
                                    } else {
                                        that.handleLeave()
                                    }
                                }
                            })
                            //}
                    }
                })
            }
        });
    },
    lalal(e) {
        let that = this;
        that.setData({
            x: e.detail.x,
            y: e.detail.y,
        });
    },
    ios_method(e) {
        let that = this;
        /*that.setData({
            x: e.detail.x,
            y: e.detail.y,
        });*/
        that.setData({
            isPreviewImage: true
        })
        if (that.data.isPreviewImage) {
            wx.previewImage({
                urls: [that.data.page_ma],
            });
        }
    },
    service(e) {
        let that = this;
        that.setData({
            service_Modal: true,
            x: e.detail.x,
            y: e.detail.y,
        })
    },
    service_hide() {
        let that = this;
        that.setData({
            service_Modal: false
        })
    },
    handleLeave() {
        let that = this;
        let timestamp = parseInt(new Date().getTime() / 1000);
        let content = {
            x: that.data.x ? that.data.x : 0,
            y: that.data.y ? that.data.y : 0,
            longitude: app.globalData.longitude ? app.globalData.longitude : 0,
            latitude: app.globalData.latitude ? app.globalData.latitude : 0,
            userinfo: wx.getStorageSync('_userinfo'),
            device_info: wx.getStorageSync('device_info')
        };
        content = JSON.stringify(content);
        let p = {};
        if (that.data.get_user_info == 0) {
            p = {
                m: 'invoke',
                type: 'jump',
                content: content,
                uid: that.data.uid,
                weappid: that.data.weappid
            };
        } else if (that.data.get_user_info == 1) {
            p = {
                m: 'invoke',
                type: 'jump',
                content: content,
                uid: that.data.uid,
                weappid: that.data.weappid
            };
        }
        if (that.data.pls) { // 自己试玩
            that.data.play_time_start = parseInt(new Date().getTime() / 1000);
            wx.setStorageSync('play_time_start_' + that.data.uid + '_' + that.data.weappid, that.data.play_time_start);

            // 发送一个去试玩的事件
            let content = {
                x: that.data.x ? that.data.x : 0,
                y: that.data.y ? that.data.y : 0,
                longitude: app.globalData.longitude ? app.globalData.longitude : 0,
                latitude: app.globalData.latitude ? app.globalData.latitude : 0,
                userinfo: wx.getStorageSync('_userinfo'),
                device_info: wx.getStorageSync('device_info'),
                pls: that.data.pls,
            };
            content = JSON.stringify(content);

            let p = {
                m: 'invoke',
                type: 'go_play',
                content: content,
                uid: that.data.uid,
                weappid: that.data.weappid
            };
            //if (0 == that.data.jfplay) {
            post(p).then(res => {
                    if (res.r == 0) {}
                })
                //}
        } else {
            that.data.jump_time_start = parseInt(new Date().getTime() / 1000);
            wx.setStorageSync('jump_time_start_' + that.data.uid + '_' + that.data.weappid, that.data.jump_time_start);

            //if (that.data.appid != wx.getStorageSync('appid_' + that.data.appid)) {
            //if (0 == that.data.jfplay) {
            post(p).then(res => {
                if (res.r == 0) {
                    wx.setStorageSync('appid_' + that.data.appid, that.data.appid)
                }
            });
            //}
            //}
        }
    },

    onHide: function() {
        let that = this;
        if (!!that.data.page_ma) {
            if (that.data.isPreviewImage) {
                that.handleLeave()
            }
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        this.data.play_time_start = 0;
        this.data.play_time_end = 0;
        this.play_time = 0;
        this.data.jump_time_start = 0;
        this.data.jump_time_end = 0;

        wx.getStorageInfo({
            success: function(res) {
                for (var i = 0; i < res.keys.length; i++) {
                    var key = res.keys[i];
                    if (0 == key.indexOf('play_time_start_')) {
                        wx.removeStorageSync(key);
                    } else if (0 == key.indexOf('play_time_end_')) {
                        wx.removeStorageSync(key);
                    } else if (0 == key.indexOf('jump_time_start_')) {
                        wx.removeStorageSync(key);
                    } else if (0 == key.indexOf('jump_time_end_')) {
                        wx.removeStorageSync(key);
                    } else if (0 == key.indexOf('pls_')) {
                        wx.removeStorageSync(key);
                    }
                }
                wx.hideLoading();
            }
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let that = this;
        app.getLbs();
        if (that.data.uid && that.data.weappid) {
            let pls = wx.getStorageSync('pls_' + that.data.uid + '_' + that.data.weappid);
            if (pls) { // 自己试玩
                that.data.pls = pls;
                let play_time_start = wx.getStorageSync('play_time_start_' + that.data.uid + '_' + that.data.weappid);
                if (play_time_start && play_time_start > 0) {
                    that.data.play_time_start = play_time_start;

                    that.data.play_time_end = parseInt(new Date().getTime() / 1000);
                    wx.setStorageSync('play_time_end_' + that.data.uid + '_' + that.data.weappid, that.data.play_time_end);
                    let play_time = that.data.play_time_end - that.data.play_time_start;
                    that.setData({
                        play_time: play_time
                    });
                    wx.showLoading({
                        title: '结算中···',
                        mask: true,
                        success: (result) => {}
                    });
                    let content = {
                        userinfo: wx.getStorageSync('_userinfo'),
                        device_info: wx.getStorageSync('device_info'),
                        pls: that.data.pls,
                        play_time: that.data.play_time
                    };
                    content = JSON.stringify(content);
                    let p = {
                        m: 'invoke',
                        type: 'play',
                        content: content,
                        uid: that.data.uid,
                        weappid: that.data.weappid
                    };
                    //if (0 == that.data.jfplay) {
                    post(p).then(res => {
                            that.setData({
                                isPreviewImage: false
                            })
                            wx.removeStorageSync('play_time_start_' + that.data.uid + '_' + that.data.weappid);
                            wx.removeStorageSync('play_time_end_' + that.data.uid + '_' + that.data.weappid);
                            wx.hideLoading();
                            if (play_time < that.data.weapp_play_time) {
                                wx.showModal({
                                    content: '任务未完成请重试!',
                                    showCancel: false
                                });
                            } else {

                            }
                        })
                        //}
                }
            } else {

                let jump_time_start = wx.getStorageSync('jump_time_start_' + that.data.uid + '_' + that.data.weappid);
                if (jump_time_start && jump_time_start > 0) {
                    that.data.jump_time_start = jump_time_start;

                    that.data.jump_time_end = parseInt(new Date().getTime() / 1000);
                    wx.setStorageSync('jump_time_end_' + that.data.uid + '_' + that.data.weappid, that.data.jump_time_end);
                    let jump_play_time = that.data.jump_time_end - that.data.jump_time_start;
                    that.setData({
                        jump_play_time: jump_play_time
                    });
                    wx.showLoading({
                        title: '结算中···',
                        mask: true,
                        success: (result) => {}
                    });
                    let content = {
                        userinfo: wx.getStorageSync('_userinfo'),
                        device_info: wx.getStorageSync('device_info')
                    };

                    content = JSON.stringify(content);

                    let p = {
                        m: 'invoke',
                        type: 'jump_back', // 分享玩（游客玩）返回
                        content: content,
                        uid: that.data.uid,
                        weappid: that.data.weappid
                    };
                    //if (0 == that.data.jfplay) {
                    post(p).then(res => {
                        that.setData({
                            isPreviewImage: false
                        })
                        wx.removeStorageSync('jump_time_start_' + that.data.uid + '_' + that.data.weappid);
                        wx.removeStorageSync('jump_time_end_' + that.data.uid + '_' + that.data.weappid);
                        wx.hideLoading();
                        if (jump_play_time < that.data.weapp_jump_play_time) {
                            wx.showModal({
                                content: '任务未完成请重试',
                                showCancel: false
                            });
                        } else {
                            that.setData({
                                ifQuestion: true,
                                aid: res.aid
                            })
                            var contentts = '';
                            var btnbg = 'background-color: #1AAD19;color: #ffffff;'
                            if (that.data.yj_type == 3) {
                                contentts = '试玩结束，请回答下列问题领取奖励';
                                btnbg = 'background-color:#fff;color:#1AAD19;border: 1px solid #1AAD19;'
                            } else if (that.data.yj_type == 2) {
                                contentts = '试玩结束，请上传截图领取奖励';
                                btnbg = 'background-color:#fff;color:#1AAD19;border: 1px solid #1AAD19;'
                            } else {
                                contentts = '试玩结束，请返回试玩其它小程序!'

                            }
                            wx.showModal({
                                //试玩结束，请返回' + that.data.name + '试玩其它小程序!
                                content: contentts,
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        that.setData({
                                            btnbg: btnbg
                                        })
                                        wx.navigateBack({
                                            delta: 1
                                        });
                                    }
                                }
                            });
                            // wx.showModal({
                            //     content: '试玩结束，请返回',
                            //     showCancel: false,
                            //     success(res) {
                            //         if (res.confirm) {
                            //             wx.navigateBack({
                            //                 delta: 1
                            //             });
                            //         }
                            //     }
                            // });
                        }
                    });
                    //}
                }
            }
        }
    },
    getKey(e) {
        let that = this;
        var val = e.detail.value;
        that.setData({
            get_key: val
        });
    },
    answer_f() {
        let that = this;
        let p = {
            m: 'answer_question',
            weappid: that.data.weappid,
            uid: that.data.uid,
            answer: that.data.get_key,
            question_id: that.data.question_id,
            aid: that.data.aid
        };
        if (that.data.jump_weapp == that.data.jumpWeapp) {
            post(p).then(res => {
                if (res.r == 0) {
                    wx.showModal({
                        content: '任务完成，请返回',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) {
                                wx.navigateBack({
                                    delta: -1
                                });
                            }
                        }
                    });
                } else {
                    wx.showModal({
                        content: res.errmsg,
                        showCancel: false
                    });
                }

            })
        }
    },
    showLoading: function() {
        wx.showToast({
            title: '结算中...',
            icon: 'loading'
        });
    },
    cancelLoading: function() {
        wx.hideToast();
    },
    /**
     * 生命周期函数--监听页面隐藏
     */

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},
    errorGg: function(e) {
        // console.log(e.detail.errCode);
        // console.log(e.detail.errMsg);
        let that = this;
        if (that.data.uid && that.data.weappid) {
            let content = {
                errCode: e.detail.errCode,
                errMsg: e.detail.errMsg,
                userinfo: wx.getStorageSync('_userinfo'),
                device_info: wx.getStorageSync('device_info')
            };

            content = JSON.stringify(content);
            let p = {
                m: 'invoke',
                type: 'ad_err',
                content: content,
                uid: that.data.uid,
                weappid: that.data.weappid
            };
            post(p).then(res => {
                // console.log(res)
            })
        }
    },
    submitFormId(e) {
        // console.log(e);
        let formId = e.detail.formId;
        let button = e.detail.target.dataset.name;
        let page = getCurrentPageUrlWithArgs();
        let that = this;
        let p = {
            m: 'colform',
            uid: that.data.uid,
            openid: wx.getStorageSync('weapp_openid'),
            formId: formId
        };
        if (page && page.length > 0) {
            p.page = page;
        }
        if (button && button.length > 0) {
            p.button = button;
        }
        post(p).then(res => {})
    }
})